package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.home.hotel.shop.HomeProductdetailActivity;
import a66weding.com.jiehuntong.beans.PushBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.Utils;

/**
 * 商品，活动推送界面
 */
public class PopGoodsActivity extends Activity implements View.OnClickListener {
    private ImageView pop_goods_img;// 商品图片
    private TextView pop_goods_look;// 查看详情
    private LinearLayout ll_goods;// 商品弹框
    private ImageView img_pop;// 活动图片
    private LinearLayout ll_img;// 活动弹框

    private Context context;
    private Intent intent;
    private PushBean pushBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pop_goods);
        context = this;
        initView();
        initInfo();
    }

    private void initView() {

        findViewById(R.id.pop_goods_cancle).setOnClickListener(this);// 取消按钮
        findViewById(R.id.img_pop_close).setOnClickListener(this);// 关闭按钮
        pop_goods_img = (ImageView) findViewById(R.id.pop_goods_img);
        pop_goods_look = (TextView) findViewById(R.id.pop_goods_look);
        pop_goods_look.setOnClickListener(this);
        ll_goods = (LinearLayout) findViewById(R.id.ll_goods);
        img_pop = (ImageView) findViewById(R.id.img_pop);
        img_pop.setOnClickListener(this);
        ll_img = (LinearLayout) findViewById(R.id.ll_img);
    }

    private void initInfo() {
        intent = getIntent();
        pushBean = (PushBean) intent.getSerializableExtra("pushBean");
//       测试数据
        /*if (pushBean == null) {
            pushBean = new PushBean();
            pushBean.setPushCode("0");
            PushBean.DataBean dataBean = new PushBean.DataBean();
            dataBean.setContent("短剑重铸之日，骑士归来之时");
            dataBean.setTitle("放逐之风");
//            dataBean.setImageUrl("http://image27.360doc.com/DownloadImg/2011/04/2015/11077777_17.png");
            dataBean.setImageUrl("http://cdn.duitang.com/uploads/item/201112/29/20111229111847_NZBhA.jpg");
//            dataBean.setImageUrl("http://pic10.nipic.com/20100930/2531170_135846730413_2.jpg");
            dataBean.setUrl("http://j.66wedding.com/news.html");
            dataBean.setProductId(2880);
            pushBean.setData(dataBean);
        }*/
        /**
         *  pushCode：0，商品；1，活动（h5不需要分享）；2，结婚趣闻（h5需要分享）；666，其它（只做显示，无跳转）
         */
        String pushCode = pushBean.getPushCode();
        if (pushCode.equals("0")) {
            ll_goods.setVisibility(View.VISIBLE);
            ll_img.setVisibility(View.GONE);
            MyBitmapTransformation myBitmapTransformation = new MyBitmapTransformation(context, pop_goods_img);
            Glide.with(this).load(pushBean.getData().getImageUrl()).placeholder(R.mipmap.bg_loadimage).transform(myBitmapTransformation).into(pop_goods_img);
        } else if (pushCode.equals("1") || pushCode.equals("2") || pushCode.equals("666")) {
            ll_img.setVisibility(View.VISIBLE);
            ll_goods.setVisibility(View.GONE);
            if (pushCode.equals("666")) {
                img_pop.setEnabled(false);
            }
            MyBitmapTransformation myBitmapTransformation = new MyBitmapTransformation(context, img_pop);
            Glide.with(this).load(pushBean.getData().getImageUrl()).placeholder(R.mipmap.bg_loadimage).transform(myBitmapTransformation).into(img_pop);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.pop_goods_cancle:
            case R.id.img_pop_close:
                finish();
                break;
            case R.id.pop_goods_look:
                String producId = pushBean.getData().getProductId();
                if(TextUtils.isEmpty(producId) || !Utils.isNumeric(producId)){
                    CUtils.showToast("产品id获取失败");
                } else {
                    intent = new Intent(this, HomeProductdetailActivity.class);
                    intent.putExtra("productId", Integer.parseInt(producId));
                    startActivity(intent);
                    finish();
                }

                break;
            case R.id.img_pop:
                intent = new Intent(this, WebViewActvity.class);
                intent.putExtra("urlPath", pushBean.getData().getUrl());
                intent.putExtra("title", pushBean.getData().getTitle());
                if (pushBean.getPushCode().equals("2")) {// 结婚趣闻需要分享
                    intent.putExtra("share", true);
                } else {
                    intent.putExtra("share", false);
                }
                startActivity(intent);
                finish();
                break;
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(msg.arg1, msg.arg2);
                    ((ImageView)msg.obj).setLayoutParams(params);
                    break;
            }
        }
    };

    class MyBitmapTransformation extends BitmapTransformation {

        private Context context;
        private ImageView imageView;
        public MyBitmapTransformation(Context context, ImageView imageView) {
            super(context);
            this.context = context;
            this.imageView = imageView;
        }

        @Override
        protected Bitmap transform(BitmapPool bitmapPool, Bitmap bitmap, int w, int h) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            if (width <= 0 || height <= 0) {
                return bitmap;
            }
            double aspectRatio = (double) height / (double) width;
            int targetWidth = imageView.getWidth();
            int targetHeight = (int) (targetWidth * aspectRatio);
            if (targetHeight > 0 && targetWidth > 0) {
                Message msg = handler.obtainMessage();
                msg.what = 0;
                msg.arg1 = targetWidth;
                msg.arg2 = targetHeight;
                msg.obj = imageView;
                handler.sendMessage(msg);
                Bitmap result = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, false);
                if (result != bitmap) {
                    // Same bitmap is returned if sizes are the same
                    bitmap.recycle();
                }
                return result;
            } else {
                return bitmap;
            }
        }

        @Override
        public String getId() {
            return getClass().getName();
        }
    }
}
